package com.controller;

import com.entity.*;
import com.service.inte.MemberServiceInte;
import com.service.inte.PartyOrgBranchServiceInte;
import com.service.inte.PartyOrgDetailServiceInte;
import com.util.TreeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Controller
public class BranchController {

    @Autowired
    private PartyOrgBranchServiceInte partyOrgBranchServiceInte;

    @Autowired
    private PartyOrgDetailServiceInte partyOrgDetailServiceInte;

    @Autowired
    private MemberServiceInte memberServiceInte;

    @PostMapping("getTblPartyOrgBranchAll")
    @ResponseBody
    public String getTblPartyOrgBranchAll() {
        List<Td_tblPartyOrgBranch> tblPartyOrgBranchall = partyOrgBranchServiceInte.getAllBrnch();

        List<TreeBuilder.Node> allNodes = new ArrayList<TreeBuilder.Node>();
        for (int i = 0; i < tblPartyOrgBranchall.size(); i++) {
            TreeBuilder.Node tb = new TreeBuilder.Node();
            tb.setFd_BranchID(tblPartyOrgBranchall.get(i).getFd_BranchID());
            tb.setText(tblPartyOrgBranchall.get(i).getFd_BranchName());
            tb.setFd_ParentBranchID(String.valueOf(tblPartyOrgBranchall.get(i).getFd_ParentBranchID()));
            tb.setFd_leveltype(Integer.getInteger(tblPartyOrgBranchall.get(i).getFd_leveltype()));
            allNodes.add(tb);
        }
        TreeBuilder tbs = new TreeBuilder(allNodes);
        String returns = tbs.buildJSONTree();
        return returns;
    }

    @GetMapping("turnBranchSearch")
    public String turnBranchSearch() {
        return "branch/branchSearch.html";
    }

    @PostMapping("getBranchSearch")
    @ResponseBody
    public List<Object[]> getBranchSearch(String name, int page) {
        return partyOrgBranchServiceInte.getByParent(145, name, page);
    }

    @GetMapping("getBranchDetail")
    public String getBranchDetail(HttpServletRequest request, int branchId) {
        request.setAttribute("detail", partyOrgBranchServiceInte.getBybranchId(branchId));
        request.setAttribute("secretary", partyOrgBranchServiceInte.getSecretaryByBranch(145));
        return "branch/detail.html";
    }

    @PostMapping("addBranch")
    public String addBranch(Td_tblPartyOrgBranch td_tblPartyOrgBranch) {
        td_tblPartyOrgBranch.setFd_applystatus(1);
        td_tblPartyOrgBranch.setFd_applydate(new Date(System.currentTimeMillis()));
        td_tblPartyOrgBranch.setFd_LinkId(1866);
        td_tblPartyOrgBranch.setFd_chairMan("张小龙");
        td_tblPartyOrgBranch.setFd_leveltype("0");
        System.out.println(partyOrgBranchServiceInte.saveOrUpdateBranch(td_tblPartyOrgBranch));
        return "member/memberPage.html";
    }

    @GetMapping("turnAddBranchSearch")
    public String turnAddBranchSearch() {
        return "branch/approve/branchSearch.html";
    }

    @GetMapping("turnAddBranch")
    public String turnAddBranch() {
        return "branch/approve/detail.html";
    }

    @PostMapping("addBranchDetail")
    @ResponseBody
    public List<Object[]> addBranchDetail(String name, int page) {
        return partyOrgBranchServiceInte.getNewBranchByParent(145, name, page);
    }

    @GetMapping("turnBranchApprove")
    public String turnBranchApprove(int branchId, HttpServletRequest request) {
        request.setAttribute("branch", partyOrgBranchServiceInte.getBybranchId(branchId));
        return "branch/approve/detailApprove.html";
    }

    @PostMapping("branchApprove")
    public String approveBranch(Td_tblPartyOrgBranch td_tblPartyOrgBranch, String reason) {
        //添加一条组织审批记录
        Td_tblPartyOrgbranchApprove partyOrgbranchApprove = new Td_tblPartyOrgbranchApprove();
        partyOrgbranchApprove.setFd_beBranchId(td_tblPartyOrgBranch.getFd_BranchID());
        partyOrgbranchApprove.setFd_beBranchName(td_tblPartyOrgBranch.getFd_BranchName());
        partyOrgbranchApprove.setFd_branchId(145);
        partyOrgbranchApprove.setFd_branchName("中共广州");
        partyOrgbranchApprove.setFd_date(new Date(System.currentTimeMillis()));
        partyOrgbranchApprove.setFd_loginId(1866);
        partyOrgbranchApprove.setFd_userName("张大龙");
        partyOrgbranchApprove.setFd_status(td_tblPartyOrgBranch.getFd_applystatus());
        partyOrgbranchApprove.setFd_reason(reason);
        partyOrgBranchServiceInte.saveOrSaveApprove(partyOrgbranchApprove);
        System.out.println("是否同意：" + td_tblPartyOrgBranch.getFd_applystatus());
        if (td_tblPartyOrgBranch.getFd_applystatus() == 0) {
            //新增领导
            System.out.println("修改了" + memberServiceInte.updateBranch(td_tblPartyOrgBranch.getFd_BranchID(), td_tblPartyOrgBranch.getFd_BranchName(), td_tblPartyOrgBranch.getFd_LinkId()) + "条记录");
            Td_tblPartyOrgDetail td_tblPartyOrgDetail = new Td_tblPartyOrgDetail();
            td_tblPartyOrgDetail.setFd_BranchID(td_tblPartyOrgBranch.getFd_BranchID());
            td_tblPartyOrgDetail.setFd_branchName(td_tblPartyOrgBranch.getFd_BranchName());
            td_tblPartyOrgDetail.setFd_username(td_tblPartyOrgBranch.getFd_chairMan());
            td_tblPartyOrgDetail.setFd_LoginID(td_tblPartyOrgBranch.getFd_LinkId());
            td_tblPartyOrgDetail.setFd_VLeaderName("党支部书记");
            td_tblPartyOrgDetail.setFd_Remark(td_tblPartyOrgBranch.getFd_BranchName());
            partyOrgDetailServiceInte.saveOrUpdate(td_tblPartyOrgDetail);

            td_tblPartyOrgBranch.setFd_applytype(0);
        }
        partyOrgBranchServiceInte.saveOrUpdateBranch(td_tblPartyOrgBranch);
        return "member/memberPage.html";
    }

    @GetMapping("turnBranchApproveLog")
    public String turnBranchApproveLog() {
        return "branch/approve/branchApproveLog.html";
    }

    @PostMapping("getBranchApproveLog")
    @ResponseBody
    public List<Object[]> getBranchApproveLog(String name, int page) {
        return partyOrgBranchServiceInte.getBeBranchByFd_branchId(145, name, page);
    }

    @PostMapping("getLowerBranch")
    @ResponseBody
    public List<Object[]> getLowerBranch(String name, int page) {
        return partyOrgBranchServiceInte.getByParentId(145, name, page);
    }

    @GetMapping("turnBranchPunishSearch")
    public String turnPunishSearch() {
        return "branch/punish/branchSearch.html";
    }

    @GetMapping("turnBranchPunishDetail")
    public String turnBranchPunishDetail(int branchId, HttpServletRequest request) {
        Td_tblPartyOrgBranch branch = partyOrgBranchServiceInte.getBybranchId(branchId);
        request.setAttribute("detail", branch);
        request.setAttribute("secretary", partyOrgBranchServiceInte.getSecretaryByBranch(branch.getFd_BranchID()));
        return "branch/punish/detail.html";
    }

    @PostMapping("addBranchPunish")
    public String addBranchPunish(Td_tblPartyOrgBranchPunish td_tblPartyOrgBranchPunish) {
        td_tblPartyOrgBranchPunish.setFd_userName("张大龙");
        td_tblPartyOrgBranchPunish.setFd_loginId(1866);
        partyOrgBranchServiceInte.saveOrUpdateBranchPunish(td_tblPartyOrgBranchPunish);
        return "member/memberPage.html";
    }

    @GetMapping("turnBranchPunishLog")
    public String turnBranchPunishLog() {
        return "branch/punish/branchPunishLog.html";
    }

    @GetMapping("turnBranchPunishLogDetail")
    public String turnBranchPunishLogDetail(int punishId, HttpServletRequest request) {
        Td_tblPartyOrgBranchPunish punish = partyOrgBranchServiceInte.getPunishById(punishId);
        request.setAttribute("detail", partyOrgBranchServiceInte.getBybranchId(punish.getFd_beBranchId()));
        request.setAttribute("punishDetail", punish);
        return "branch/punish/branchPunishLogDdetail.html";
    }

    @PostMapping("getBranchPunish")
    @ResponseBody
    public List<Object[]> getBranchPunish(String branchName, int page) {
        return partyOrgBranchServiceInte.getPunishByLoginId(1866, branchName, page);
    }

    @GetMapping("turnNowLeader")
    public String turnNowLeader(HttpServletRequest request) {
        request.setAttribute("branch", partyOrgBranchServiceInte.getBybranchId(145));
        request.setAttribute("leader", partyOrgBranchServiceInte.getAllLeaderByBranchId(145));
        request.setAttribute("leaderChangeLog", partyOrgBranchServiceInte.getLastChangeLeader(145));
        return "branch/election/nowLeader.html";
    }

    @GetMapping("turnElection")
    public String turnElection(HttpServletRequest request) {
        request.setAttribute("branch", partyOrgBranchServiceInte.getBybranchId(145));
        request.setAttribute("leader", partyOrgBranchServiceInte.getAllLeaderByBranchId(145));
        request.setAttribute("leaderChangeLog", partyOrgBranchServiceInte.getLastChangeLeader(145));
        return "branch/election/election.html";
    }

    @PostMapping("saveElection")
    public boolean saveElection(String VLeaderName1, String VLeaderName2, String VLeaderName3, String VLeaderId1, String VLeaderId2, String VLeaderId3, Date lastTime, String type) {
//        if (partyOrgDetailServiceInte.deleteByFd_BranchID(145) > 0) {
            String VleaderName = VLeaderName1.split(",")[0];
            int leaderId1=Integer.valueOf(VLeaderId1.split(",")[0]);
            String[] leaderName2 = VLeaderName2.split(",");
            String[] leaderId2 = VLeaderId2.split(",");
            String[] leaderName3 = VLeaderName3.split(",");
            String[] leaderId3 = VLeaderId3.split(",");
        System.out.println(leaderId2.length);
            List<Td_tblPartyOrgDetail> partyOrgDetails2 = new ArrayList<>();
            Td_tblPartyOrgDetail orgDetail = new Td_tblPartyOrgDetail();
            orgDetail.setFd_branchName("供电局");
            orgDetail.setFd_BranchID(145);
            orgDetail.setFd_LoginID(leaderId1);
            orgDetail.setFd_username(VleaderName);
            orgDetail.setFd_VLeaderName("党支部书记");
            partyOrgDetailServiceInte.saveOrUpdate(orgDetail);
            ArrayList<Td_tblPartyOrgDetail> partyOrgDetails3 = new ArrayList<>();
            for (int i = 0; i < leaderName2.length-1; i++) {
                Td_tblPartyOrgDetail partyOrgDetail = new Td_tblPartyOrgDetail();
                partyOrgDetail.setFd_VLeaderName("党支部副书记");
                partyOrgDetail.setFd_username(leaderName2[i]);
                partyOrgDetail.setFd_LoginID(Integer.valueOf(leaderId2[i]));
                partyOrgDetail.setFd_branchName("供电局");
                partyOrgDetail.setFd_BranchID(145);
                partyOrgDetails2.add(partyOrgDetail);
            }
            for (int i = 0; i < leaderName3.length-1; i++) {
                Td_tblPartyOrgDetail partyOrgDetail = new Td_tblPartyOrgDetail();
                partyOrgDetail.setFd_VLeaderName("党支部委员");
                partyOrgDetail.setFd_username(leaderName3[i]);
                partyOrgDetail.setFd_LoginID(Integer.valueOf(leaderId3[i]));
                partyOrgDetail.setFd_branchName("供电局");
                partyOrgDetail.setFd_BranchID(145);
                partyOrgDetails3.add(partyOrgDetail);
            }

            partyOrgDetailServiceInte.saveAll(partyOrgDetails2);
            partyOrgDetailServiceInte.saveAll(partyOrgDetails3);
            TD_tblPartychangelog partychangelog = new TD_tblPartychangelog();
            partychangelog.setFd_BranchID(145);
            partychangelog.setFd_branchdesc("供电局");
            partychangelog.setFd_changetype(Integer.valueOf(type));
            partychangelog.setFd_destmenber("书记：" + VLeaderName1 + "\n副书记：" + VLeaderName2 + "\n委员：" + VLeaderName3);
            partychangelog.setFd_date(new Date(System.currentTimeMillis()));
            partychangelog.setFd_loginId(1866);
            partychangelog.setFd_userName("张大龙");
            partychangelog.setFd_lastchangedate(lastTime);
            partyOrgBranchServiceInte.saveOrUploadBranchChangeLog(partychangelog);
//        } else {
//
//        }
        return true;
    }
}
